/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.view;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import me.kaede.tagview.OnTagClickListener;
import me.kaede.tagview.Tag;
import me.kaede.tagview.TagView;
import net.tsz.afinal.json.JsonMap;
import so.shanku.essential.R;
import so.shanku.essential.app.MyApplication;
import so.shanku.essential.getdata.JsonKeys;
import so.shanku.essential.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示商品参数的布局
 *
 * @author 亚明
 */
public class LayoutProductInfoParams extends LinearLayout {

    private Context context;
    /**
     * 参数的标题
     */
    private TextView tv_title;
    /**
     * 参数信息的展示
     */
    private TagView pipg_info;
    /**
     * 展示的参数信息
     */
    private List<JsonMap<String, Object>> data;

    /**
     * 当前选中的参数的view的索引id
     */
    private int tv_position;

    /**
     * 当前的参数有改变的事件
     */
    private MyOnItemClickListener itemClickListener;

    /**
     * 当前的参数有改变的事件
     */
    public void setItemClickListener(MyOnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    /**
     * 当前选中的参数的view的索引id
     */
    public int getTv_position() {
        return tv_position;
    }

    /**
     * 展示的参数信息
     */
    public List<JsonMap<String, Object>> getData() {
        return data;
    }

    LayoutInflater inflater;

    public LayoutProductInfoParams(Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {

        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_product_info_params, this);
        tv_title = (TextView) findViewById(R.id.l_p_i_tv_pro_param);
        pipg_info = (TagView) findViewById(R.id.tagview);
        pipg_info.setOnTagClickListener(new OnTagClickListener() {
                                            @Override
                                            public void onTagClick(View view, Tag tag, int position) {
                                                if (tv_position > -1) {
                                                    pipg_info.getChildAt(tv_position).setSelected(false);

                                                }
                                                view.setSelected(true);
                                                tv_position = position;
                                                pipg_info.getChildAt(tv_position).setSelected(true);
                                                if (null != itemClickListener) {
                                                    itemClickListener.itemClick(view, position);
                                                }
                                            }
                                        }

        );
    }

    /**
     * 设定分类的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.tv_title.setText(title);
    }

    /**
     * 设定这个分类下的数据
     */
    public void setParamsData(List<JsonMap<String, Object>> data,
                              int defaultSelectPosition) {
        this.data = data;
        pipg_info.removeAllTags();
        tv_position = -1;
        for (int i = 0; i < data.size(); i++) {
            JsonMap item = data.get(i);
            Tag tag = new Tag(item.getInt("paramId"), item.getStringNoNull("paramName"));
            tag.background = this.getResources().getDrawable(R.drawable.cus_black_white_bt);
            tag.tagTextColor = R.drawable.goods_params_txt;
            tag.radius = 0f;
            pipg_info.addTag(tag);
            if (item.getBoolean("isChecked")) {
                tv_position = i;
            }
        }

        if (tv_position >= 0) {
            //JsonMap item =data.get(tv_position);
            int num = pipg_info.getChildCount();
            LogUtil.d("sss", num);
            //pipg_info.getChildAt(tv_position).setSelected(true);
        }
    }


//e

    /**
     * 参数改变的事件
     *
     * @author 亚明
     */
    public interface MyOnItemClickListener {
        void itemClick(View tv, int position);
    }
}
